package com.engine.zhlx.financemoduel.workflow.utils;


import org.apache.axis.encoding.Base64;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import weaver.docs.webservices.DocAttachment;
import weaver.docs.webservices.DocInfo;
import weaver.docs.webservices.DocServiceImpl;
import weaver.general.BaseBean;
import weaver.hrm.User;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-16
 * @Description: 导出ExcelUtil
 * @ModifyLog:
 **/

public class ExcelUtil {
    /**
     * 生成Excel 并放到指定位置
     *@param titlelist 标题名称list
     *@param zdlist 字段list
     *@param datalist 数据list
     *@return 是否正常生成
     *@throws IOException
     *@author:
     * (titlelist  和  zdlist  顺序要一致, 要一一对应)
     */
    public static int createTaxInfoExcel(String sheetname,int secid,List<String> titlelist, List<String> zdlist, List<Map<String,Object>> datalist) {
        int docid = -1;
        String filename = sheetname+".xls";
        try {
            //创建HSSFWorkbook对象(excel的文档对象)
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet(sheetname);
            HSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment( HorizontalAlignment.CENTER);
            cellStyle.setBorderRight( BorderStyle.THIN);
            cellStyle.setBorderLeft( BorderStyle.THIN);
            cellStyle.setBorderTop( BorderStyle.THIN);
            cellStyle.setBorderBottom( BorderStyle.THIN);
            cellStyle.setWrapText(true);
            HSSFFont font = wb.createFont();
            font.setFontName("黑体");
            font.setFontHeightInPoints((short) 14);
            cellStyle.setFont(font);
            HSSFRow row = sheet.createRow(0);
            Cell cell;
            // 添加表头
            for(int i = 0;i<titlelist.size();i++){
                sheet.setColumnWidth(i, 3766);
                cell = row.createCell(i);
                cell.setCellValue(titlelist.get(i));
                cell.setCellStyle( cellStyle );
            }
            System.out.println("datalist="+datalist.size());
            for(int j = 0;j<datalist.size();j++){
                sheet.setColumnWidth(j+1, 3766);
                row = sheet.createRow(j+1);
                Map<String,Object> data = datalist.get(j);
                for(int col = 0;col<zdlist.size();col++){
                    cell = row.createCell(col);
                    cell.setCellStyle( cellStyle );
                    if(data!=null&&data.get(zdlist.get(col))!=null){
                        cell.setCellValue(String.valueOf(data.get(zdlist.get(col))));
                    }
                }
            }
            //输出Excel文件1
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] content = os.toByteArray();
            docid = createDoc(filename,content,secid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return docid;
    }

    public static int generateTaxInfoData(String sheetname,int secid,List<Map<String,Object>> datalist){
        List<String> titlelist = new ArrayList<>();
        titlelist.add("序号");
        titlelist.add("采购申请的项目编号");
        titlelist.add("创建对象的人员名称");
        titlelist.add("最后更改的日期");
        titlelist.add("申请人工号");
        titlelist.add("短文本");
        titlelist.add("物料编号");
        titlelist.add("工厂");
        titlelist.add("库存地点");
        titlelist.add("需求跟踪号");
        titlelist.add("物料组");
        titlelist.add("采购申请数量");
        titlelist.add("采购申请计量单位");
        titlelist.add("项目交货日期");
        titlelist.add("采购申请中的价格");
        titlelist.add("价格单位");
        titlelist.add("采购凭证中的项目类别");
        titlelist.add("科目分配类别");
        titlelist.add("采购组织");
        titlelist.add("成本中心");
        titlelist.add("WBS项目号");
        titlelist.add("采购申请号");
        List<String> zdlist = new ArrayList<>();
        zdlist.add( "xh");
        zdlist.add( "cgsqdxmbh");
        zdlist.add( "cjdxdrymc" );
        zdlist.add( "zhggdrq" );
        zdlist.add( "sqrgh" );
        zdlist.add( "dwb" );
        zdlist.add( "wlbh" );
        zdlist.add( "gc" );
        zdlist.add( "kcdd" );
        zdlist.add( "xqgzh" );
        zdlist.add( "wlz" );
        zdlist.add( "cgsqsl" );
        zdlist.add( "cgsqjldw" );
        zdlist.add( "xmjhrq" );
        zdlist.add( "cgsqzdjg" );
        zdlist.add( "jgdw" );
        zdlist.add( "cgpzzdxmlb" );
        zdlist.add( "kmfplb" );
        zdlist.add( "cgzz" );
        zdlist.add( "cbzx" );
        zdlist.add( "wbsxmh" );
        zdlist.add( "cgsqh" );
        return createTaxInfoExcel(sheetname,secid,titlelist,zdlist,datalist);
    }
    /*
     *@Description:创建文档
     */
    public static int createDoc(String rawFileName, byte[] content, int secid) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>>>createDocs>>>>>>" );
        int docid = -1;
        DocServiceImpl docserviceimpl = new DocServiceImpl();
        DocInfo doc = new DocInfo(); //创建文档
        String keyword = ""; //文档内容
        try {
            DocAttachment[] docattachment = new DocAttachment[1];
            baseBean.writeLog( ">>>>>rawFileName=" + rawFileName );
            String filetype = "";
            if (rawFileName.lastIndexOf( "." ) > -1) {
                filetype = rawFileName.substring( rawFileName.lastIndexOf( "." ) + 1 );
            }
            // 上传附件，创建html文档
            DocAttachment da = new DocAttachment();
            da.setFilename( rawFileName );//附件标题
            da.setFilecontent( Base64.encode( content ) ); //附件内容
            da.setFiletype( filetype ); //imagefiletype
            da.setImagefilename( rawFileName );// 附件标题
            da.setIszip( 1 );//是否压缩
            da.setIsAesEncrype( 0 );//是否加密
            da.setAesCode( "0" );
            da.setIsextfile( "1" ); //附件是否是office类型

            if (filetype.equalsIgnoreCase( "doc" )) {
                da.setDocfiletype( "3" );
            } else if (filetype.equalsIgnoreCase( "xls" )) {
                da.setDocfiletype( "4" );
            } else if (filetype.equalsIgnoreCase( "ppt" )) {
                da.setDocfiletype( "5" );
            } else if (filetype.equalsIgnoreCase( "wps" )) {
                da.setDocfiletype( "6" );
            } else if (filetype.equalsIgnoreCase( "docx" )) {
                da.setDocfiletype( "7" );
            } else if (filetype.equalsIgnoreCase( "xlsx" )) {
                da.setDocfiletype( "8" );
            } else if (filetype.equalsIgnoreCase( "pptx" )) {
                da.setDocfiletype( "9" );
            } else {
                da.setDocfiletype( "2" );
            }
            docattachment[0] = da;
            keyword = rawFileName;
            doc.setAttachments( docattachment );
            doc.setSeccategory( secid );//子目录id
            doc.setDocSubject( keyword );//标题
            doc.setDoccontent( "" );//文档内容
            /*文档属性*/
            doc.setDocType( 1 );
            //文档状态
            doc.setDocStatus( 1 );
            doc.setDocStatusStr( "正常" );
            //新闻类型
            doc.getDocPublishType();
            doc.setPublishable( "文档" );
            doc.setKeyword( keyword );
            docid = docserviceimpl.createDocByUser( doc, new User( 1 ) );
            baseBean.writeLog( ">>>>>docid=" + docid );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return docid;
    }


}
